package com.shuke.my_test.shardingSphere.manager;


import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * @author 舒克、舒克
 * @date 2025/7/25 10:41
 * @description 用户信息分表算法类
 */
@Configuration
public class UserInfoAlgorithm implements StandardShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> preciseShardingValue) {
        Long id = preciseShardingValue.getValue();
        String logicTableName = preciseShardingValue.getLogicTableName();
        // id 为 null 查询所有表
        if (id == null) {
            return logicTableName;
        }
        // 根据id 分表 动态生成表名
        String tableName = logicTableName + "_" +  id;
        if (availableTargetNames.contains(tableName)) {
            return tableName;
        }else {
            return logicTableName;
        }

    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> rangeShardingValue) {
        return List.of();
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}
